package com.dllo.lanou.cookapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.activity.BookDescritionActivity;
import com.dllo.lanou.cookapp.adapter.BookListAdapter;
import com.dllo.lanou.cookapp.entity.BookList;
import com.dllo.lanou.cookapp.entity.BookListEntity;
import com.dllo.lanou.cookapp.entity.HeadViewPageEntity;
import com.dllo.lanou.cookapp.utils.NetworkImageHolderView;
import com.dllo.lanou.cookapp.volly.VolleySingleton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xijinhua on 2016/3/19.
 */
public class BookListFragment extends Fragment {
private List<BookListEntity>  bookListEntities;
    @Bind(R.id.book_gridview)
    GridView bookGridView;
    private BookListAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book,null);
        ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bookListEntities = new ArrayList<>();
        adapter = new BookListAdapter(getActivity());
        requestData();
        bookGridView.setAdapter(adapter);
        bookGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookListEntity book  = bookListEntities.get(position);
                Intent intent = new Intent(getActivity(), BookDescritionActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("book",book);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }


    //请求数据
    public void requestData() {
        String path = " http://apis.baidu.com/tngou/book/list";
         String httpArg = "id=5&page=1&rows=21";
       String url = path + "?" + httpArg;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
               Log.i("书籍的的数据",response);
                Gson gson = new Gson();
                BookList bookList = gson.fromJson(response,BookList.class);
                if (bookList != null){
                    bookListEntities = bookList.getList();
                    adapter.addDatas(bookListEntities);

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "4ee0087cd4103e3acbab0f8efbf0bbd1");
                return map;
            }


        };
        VolleySingleton.getInstance().addRequest(request);
    }
}
